<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="my" uri="myTag/core"%>
<!DOCTYPE html>
<html>
	<head>
		<title>调查分类列表</title>
		<%@include file="/script/myJs/common.jspf"%>
	</head>
	<body>
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'west',border:false" style="width: 240px;padding:5px">
				<div class="easyui-panel" data-options="fit:true">
					<ul id="surveyTypeTree"></ul>
					<div id="surveyTypeTreeMenu" class="easyui-menu" style="width:120px;">
						<my:auth url="surveyType/toAdd"><div onclick="toSurveyTypeAdd()" data-options="iconCls:'icon-add'">添加</div></my:auth>
						<my:auth url="surveyType/toEdit"><div onclick="toSurveyTypeEditForMenu()" data-options="iconCls:'icon-edit'">修改</div></my:auth>
						<div class="menu-sep"></div>
						<my:auth url="surveyType/doDel"><div onclick="doSurveyTypeDelForMenu()" data-options="iconCls:'icon-remove'">删除</div></my:auth>
						<div class="menu-sep"></div>
						<div onclick="surveyTypeTreeFlush()" data-options="iconCls:'icon-reload'">刷新</div>
					</div>
				</div>
			</div>
			<div data-options="region:'center',border:false">
				<div class="easyui-layout" data-options="fit:true">
					<div data-options="region:'center',border:false">
						<%-- 调查分类查询条件 --%>
						<div id="surveyTypeToolbar" style="padding:0 30px;">
							<div class="conditions">
								<form id="surveyTypeQueryForm">
									<input type="hidden" id="surveyType_one" name="one">
									<span class="con-span">名称：</span>
									<input name="two" class="easyui-textbox" style="width:166px;height:35px;line-height:35px;">
									<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-search" data-options="selected:true" onclick="surveyTypeQuery();">查询</a>
									<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-clear" onclick="surveyTypeReset();">重置</a>
								</form>
							</div>
							<div class="opt-buttons">
								<my:auth url="surveyType/toAdd"><a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-add" data-options="selected:true" onclick="toSurveyTypeAdd();">添加</a></my:auth>
								<my:auth url="surveyType/toEdit"><a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="toSurveyTypeEditForBtn();">修改</a></my:auth>
								<my:auth url="surveyType/doDel"><a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-remove" onclick="doSurveyTypeDelForBtn();">删除</a></my:auth>
								<my:auth url="surveyType/toMove"><a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="toSurveyTypeMoveForBtn();">移动</a></my:auth>
							</div>
						</div>
						<%-- 调查分类数据表格 --%>
						<table id="surveyTypeGrid">
						</table>
					</div>
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		//定义变量
		var surveyTypeGrid = $("#surveyTypeGrid"); //调查分类表格对象
		var surveyTypeQueryForm = $("#surveyTypeQueryForm"); //调查分类查询对象
		var surveyTypeTree = $("#surveyTypeTree"); //调查分类树对象
		var curSelSurveyTypeId = ""; //当前选中的调查分类ID
		var curSelSurveyTypeName = ""; //当前选中的调查分类名称
	
		//页面加载完毕，执行如下方法：
		$(function() {
			initSurveyTypeGrid();
			initSurveyTypeTree();
		});
	
		//初始化调查分类表格
		function initSurveyTypeGrid() {
			surveyTypeGrid.datagrid( {
				url : "",
				onDblClickRow : <my:auth url="surveyType/toEdit">toSurveyTypeEditForDblClick</my:auth>,
				toolbar : "#surveyTypeToolbar",
				columns : [[ 
						{field : "ID", title : "", checkbox : true}, 
						{field : "NAME", title : "名称", width : 80, align : "center"},
						{field : "PARENT_NAME", title : "上级调查分类 ", width : 80, align : "center"},
						{field : "NO", title : "排序", width : 80, align : "center"}
						]]
			});
		}
		
		//初始化调查分类树
		function initSurveyTypeTree(){
			surveyTypeTree.tree({
				idFiled : "ID",
				textFiled : "NAME",
				parentField : "PARENT_ID",
				iconClsFiled : "ICON",
				checkedFiled : "CHECKED",
				lines : true,
			    url : "surveyType/treeList",
				<my:auth url="surveyType/toMove">dnd : true,
				onStopDrag : toSurveyTypeMoveForMenu,</my:auth>
				onContextMenu : function(e, node){
					e.preventDefault();
					$(this).tree("select", node.target);
					$("#surveyTypeTreeMenu").menu("show", {
						left : e.pageX,
						top : e.pageY
					})
				},
				onSelect : function(node){
					curSelSurveyTypeId = node.ID;
					curSelSurveyTypeName = node.NAME;
					$("#surveyType_one").val(curSelSurveyTypeId);
					surveyTypeGrid.datagrid("uncheckAll");
					surveyTypeGrid.datagrid("reload", $.fn.my.serializeObj(surveyTypeQueryForm));
				},
				onLoadSuccess : function(node, data){
					if(!curSelSurveyTypeId || !surveyTypeGrid.datagrid("options").url){//如果是第一次
						curSelSurveyTypeId = 1;
						surveyTypeGrid.datagrid("options").url = "surveyType/list";
					}
					
					var node = surveyTypeTree.tree("find", curSelSurveyTypeId);
					if(!node){
						curSelSurveyTypeId = 1;
						node = surveyTypeTree.tree("find", curSelSurveyTypeId);
					}
					surveyTypeTree.tree("select", node.target);
				}
			});
		}
	
		//调查分类查询
		function surveyTypeQuery() {
			surveyTypeGrid.datagrid("uncheckAll");
			surveyTypeGrid.datagrid("load", $.fn.my.serializeObj(surveyTypeQueryForm));
		}
	
		//调查分类重置
		function surveyTypeReset() {
			surveyTypeQueryForm.form("reset");
			surveyTypeQuery();
		}
		
		//到达添加调查分类页面
		function toSurveyTypeAdd() {
			if(!curSelSurveyTypeId){
				parent.$.messager.alert("提示消息", "请选择上级调查分类！", "info");
				return;
			}
			
			var surveyTypeAddDialog;
			surveyTypeAddDialog = $("<div/>").dialog({
				title : "添加调查分类",
				href : "surveyType/toAdd",
				buttons : [{
					text : "添加", 
					iconCls : "icon-add", 
					selected : true, 
					handler : function (){
						doSurveyTypeAdd(surveyTypeAddDialog);
					}
				}],
				onLoad : function() {
					$("#surveyType_parentId").val(curSelSurveyTypeId);
					$("#surveyType_parentName").val(curSelSurveyTypeName);
					$("#surveyType_name").textbox({
						required : true,
						validType : ["length[1, 16]"]
					});
					$("#surveyType_no").numberspinner({
						required : true,
						min : 1,
						max : 100
					});
				}
			});
		}
		
		//完成添加调查分类
		function doSurveyTypeAdd(surveyTypeAddDialog) {
			var surveyTypeEditFrom = $("#surveyTypeEditFrom");
			if(!surveyTypeEditFrom.form("validate")){
				return;
			}
			$.messager.confirm("确认消息", "确定要添加？", function(ok) {
				if (!ok) {
					return;
				}
				
				$.messager.progress();
				surveyTypeEditFrom.form("submit", {
					url : "surveyType/doAdd",
					success : function(data) {
						surveyTypeTree.tree("reload");
						$.messager.progress("close"); 
						
						var obj = $.parseJSON(data);
						if(!obj.success){
							parent.$.messager.alert("提示消息", obj.message, "info");
							return;
						}
						
						surveyTypeAddDialog.dialog("close");
					}
				});
			})
		}
		
		//到达修改调查分类页面
		function toSurveyTypeEdit(id){
			var surveyTypeEditDialog;
			surveyTypeEditDialog = $("<div/>").dialog({
				title : "修改调查分类",
				href : "surveyType/toEdit",
				queryParams : {"id" : id},
				buttons : [{
					text : "修改", 
					iconCls : "icon-edit", 
					selected : true, 
					handler : function (){
						doSurveyTypeEdit(surveyTypeEditDialog);
					}
				}],
				onLoad : function() {
					$("#surveyType_name").textbox({
						required : true,
						validType : ["length[1, 16]"]
					});
					$("#surveyType_no").numberspinner({
						required : true,
						min : 1,
						max : 100
					});
				}
			});
		}
		
		//到达修改调查分类页面
		function toSurveyTypeEditForBtn(){
			var surveyTypeGridRows = surveyTypeGrid.datagrid("getChecked");
			if(surveyTypeGridRows.length != 1){
				parent.$.messager.alert("提示消息", "请选择一行数据！", "info");
				return;
			}
			
			toSurveyTypeEdit(surveyTypeGridRows[0].ID);
		}
		
		//到达修改调查分类页面
		function toSurveyTypeEditForDblClick(rowIndex, rowData){
			surveyTypeGrid.datagrid("uncheckAll");
			surveyTypeGrid.datagrid("checkRow", rowIndex);
			toSurveyTypeEditForBtn();
		}
		
		//到达修改调查分类页面
		function toSurveyTypeEditForMenu() {
			toSurveyTypeEdit(curSelSurveyTypeId);
		}
		
		//完成修改调查分类
		function doSurveyTypeEdit(surveyTypeEditDialog) {
			var surveyTypeEditFrom = $("#surveyTypeEditFrom");
			if(!surveyTypeEditFrom.form("validate")){
				return;
			}
			$.messager.confirm("确认消息", "确定要修改？", function(ok) {
				if (!ok) {
					return;
				}
				
				$.messager.progress();
				surveyTypeEditFrom.form("submit", {
					url : "surveyType/doEdit",
					success : function(data) {
						surveyTypeTree.tree("reload");
						$.messager.progress("close"); 
						
						var obj = $.parseJSON(data);
						if(!obj.success){
							parent.$.messager.alert("提示消息", obj.message, "info");
							return;
						}
						
						surveyTypeEditDialog.dialog("close");
					}
				});
			})
		}
		
		//完成删除调查分类
		function doSurveyTypeDel(params) {
			$.messager.confirm("确认消息", "确定要删除？", function(ok){
				if (!ok){
					return;
				}
				
				$.messager.progress();
				$.ajax({
					url : "surveyType/doDel",
					data : params,
					success : function(obj){
						surveyTypeTree.tree("reload");
						$.messager.progress("close");
						
						if(!obj.success){
							parent.$.messager.alert("提示消息", obj.message, "info");
						}
					}
				});
			});
		}
		
		//完成删除调查分类
		function doSurveyTypeDelForBtn() {
			//校验数据有效性
			var surveyTypeGridRows = surveyTypeGrid.datagrid("getChecked");
			if(surveyTypeGridRows.length == 0){
				parent.$.messager.alert("提示消息", "请选择一行或多行数据！", "info");
				return;
			}
			
			//删除
			doSurveyTypeDel($.fn.my.serializeField(surveyTypeGridRows));
		}
		
		//完成删除调查分类
		function doSurveyTypeDelForMenu() {
			var params = {"ids" : curSelSurveyTypeId};
			var parentNode = surveyTypeTree.tree("find", curSelSurveyTypeId);
			curSelSurveyTypeId = parentNode.PARENT_ID;
			
			doSurveyTypeDel(params);
		}
		
		//到达移动调查分类页面
		function toSurveyTypeMoveForBtn(){
			var surveyTypeGridRows = surveyTypeGrid.datagrid("getChecked");
			if(surveyTypeGridRows.length != 1){
				parent.$.messager.alert("提示消息","请选择一行数据！", "info");
				return;
			}
			
			var surveyTypeDialog;
			surveyTypeDialog = $("<div/>").dialog({
				title : "选择调查分类",
				width : 300,
				height : 400,
				href : "surveyType/toMove",
				buttons : [{
					text : "确定", 
					iconCls : "icon-ok", 
					selected : true,
					handler : function (){
						var surveyTypeMoveNode = $("#surveyTypeMoveTree").tree("getSelected");
						if(!surveyTypeMoveNode){
							parent.$.messager.alert("提示消息", "请选择一行数据！", "info");
							return;
						}
						
						var sourceId = surveyTypeGridRows[0].ID;
						var sourceName = surveyTypeGridRows[0].NAME;
						var targetId = surveyTypeMoveNode.ID;
						var targetName = surveyTypeMoveNode.NAME;
						if(sourceId == targetId){
							parent.$.messager.alert("提示消息", "源调查分类和目标调查分类一致！", "info");
							return;
						}
						if(surveyTypeMoveNode.PARENT_SUB.indexOf(surveyTypeGridRows[0].PARENT_SUB) >= 0){
							parent.$.messager.alert("提示消息", "父调查分类不能移动到子调查分类下！", "info");
							return;
						}
						
						doSurveyTypeMove(sourceId, sourceName, targetId, targetName, surveyTypeDialog);
					}
				}],
				onLoad : function(){
					var surveyTypeMoveTree = $("#surveyTypeMoveTree"); 
					surveyTypeMoveTree.tree({
						idFiled : "ID",
						textFiled : "NAME",
						parentField : "PARENT_ID",
						iconClsFiled : "ICON",
						checkedFiled : "CHECKED",
						lines : true,
					    url : "surveyType/moveSurveyTypeTreeList",
					});
				}
			});
		}

		//到达移动调查分类页面
		function toSurveyTypeMoveForMenu(node){
			//校验数据有效性
			var targetNode = surveyTypeTree.tree("getParent", node.target);
			if(!targetNode){//拖动时有bug，有时会拖到空白地方。
				return;
			}
			var sourceId = node.ID;
			var sourceName = node.NAME;
			var sourceParentId = node.PARENT_ID;
			var targetId = targetNode.ID;
			var targetName = targetNode.NAME;
			if(sourceParentId == targetId){ //如果移动后的调查分类和源调查分类一样，不处理
				return;
			}
			
			//移动调查分类
			doSurveyTypeMove(sourceId, sourceName, targetId, targetName);
		}
		
		//完成移动调查分类
		function doSurveyTypeMove(sourceId, sourceName, targetId, targetName, dialog){
			$.messager.confirm("确认消息", "确定要移动【"+sourceName+"】到【"+targetName+"】？", function(ok){
				if (!ok){
					if(!dialog){
						surveyTypeTree.tree("reload");//拖动后有bug，可能会错位，所以刷新一次。
					}
					return;
				}
				
				var params = {"sourceId" : sourceId, "targetId" : targetId};
				$.messager.progress();
				$.ajax({
					url : "surveyType/doMove",
					data : params,
					success : function(obj){
						surveyTypeTree.tree("reload");
						if(dialog){
							dialog.dialog("close");
						}
						$.messager.progress("close");
						
						if(!obj.success){
							parent.$.messager.alert("提示消息", obj.message, "info");
						}
					}
				});
			});
		}
		
		//刷新调查分类
		function surveyTypeTreeFlush(){
			surveyTypeTree.tree("reload");
		}
	</script>
</html>